#include "LedCore.h"

LedCore::LedCore(QObject* parent) :
    QObject(parent),
	_fps(1),
	_ledNumber(0),
	_Layout( new TvLayout ),
    _changeMutex( new QMutex ),
    _waitTimer(new QTimer(this))
{
    _waitTimer->setInterval(1000/_fps);
    connect(_waitTimer,SIGNAL(timeout()),this,SLOT(onSendData()));

    if(!_port.open("\\\\.\\COM25")) qDebug("Can't open port");
}

QList<Led*>* LedCore::getLedList(){
	return &_LedList;
}

bool LedCore::setFps(int fps){
	if(fps <=0 || fps > 50) return false;
	_fps = fps;
    _waitTimer->stop();
    _waitTimer->setInterval(1000/_fps);
    _waitTimer->start();
	return true;
}

void LedCore::onSendData(){
    _changeMutex->lock();
    _port.setLeds(_LedList);
    _port.sendData();
    _changeMutex->unlock();
}

void LedCore::setColor(int led, QColor color){
    if(_LedList.size() <= led) return;
    _changeMutex->lock();
    _LedList.at(led)->setColor(color);
    _changeMutex->unlock();
}

void LedCore::start(){

}

void LedCore::stop(){

}
